package com.pfa.test.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.pfa.test.entities.ClientDsi;
import com.pfa.test.entities.DemandeReparation;
import com.pfa.test.entities.Materiel;
import com.pfa.test.entities.Panne;

public class ClientDaoImpl implements IClientDao {
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public void ajouterReparation(DemandeReparation dr) {
		// TODO Auto-generated method stub
		em.persist(dr);
	}

	@Override
	public List<DemandeReparation> listReparation(Long idClient) {
		Query req = em.createQuery("select r from DemandeReparation r where r.client.id=:x");
		req.setParameter("x", idClient);
		return req.getResultList();
	}

	@Override
	public DemandeReparation getReparation(Long idRep) {
		// TODO Auto-generated method stub
		return em.find(DemandeReparation.class, idRep);
	}
	
	@Override
	public void supprimerReparation(DemandeReparation rep){
		em.remove(rep);
	}

	@Override
	public void modiferReparation(DemandeReparation dmd) {
		// TODO Auto-generated method stub
		em.merge(dmd);
	}

	@Override
	public ClientDsi getClient(String cin) {
		Query req = em.createQuery("select c from ClientDsi c where c.cin like :x");
		req.setParameter("x", cin);
		return (ClientDsi) req.getSingleResult();
	}
	@Override
	public List<Materiel> listMateriel() {
		Query req = em.createQuery("select m from Materiel m");
		return req.getResultList();
	}
	@Override
	public List<Panne> listPanne() {
		Query req = em.createQuery("select p from Panne p");
		return req.getResultList();
	}
	public Materiel getMaterielByType(String type){
		Query req = em.createQuery("select m from Materiel m where m.typeMateriel like :x");
		req.setParameter("x", type);
		return (Materiel) req.getSingleResult();
	}
	public Panne getPanneByType(String nom){
		Query req = em.createQuery("select p from Panne p where p.nomPanne like :x");
		req.setParameter("x", nom);
		return (Panne) req.getSingleResult();
	}
}
